#!/bin/bash

source /home/shell/util.sh

function install () {
    requireJava
    source /etc/profile.d/java.sh
    printInfo "Maven 版本列表："
    curl -sL https://mirrors.bfsu.edu.cn/apache/maven/maven-3/|grep DIR|grep -v PARENTDIR|perl -pe "s/.*href=\"(.*?)\/\".*/\1/g"
    while [ true ]; do
        read -p "选择版本：" version
        if [[ -z "${version}" ]]; then
            printFail "请重新输入版本！"
            continue;
        else
            break;
        fi
    done
    download /opt/apache-maven.tar.gz https://mirrors.bfsu.edu.cn/apache/maven/maven-3/${version}/binaries/apache-maven-${version}-bin.tar.gz

    tar -xf /opt/apache-maven.tar.gz -C /opt/
    rm -rf /opt/apache-maven.tar.gz
    mv /opt/apache-maven-* /opt/apache-maven
    if [ ! -f "/usr/bin/mvn" ]; then
        ln -s /opt/apache-maven/bin/mvn /usr/bin
    fi
}


function config () {
    cp -r /home/shell/support/maven_settings.xml /opt/apache-maven/conf/settings.xml
cat<<EOF
1. 使用阿里云
2. 使用自定义仓库
EOF
    read -p "请选择：" repo_select
    case "${repo_select}" in
        1)
            sed -ri "s@<activeProfile>dev</activeProfile>@<\!-- <activeProfile>dev</activeProfile> -->@g" /opt/apache-maven/conf/settings.xml
        ;;
        2)
            read -p "请输入私有仓库地址[主机:端口]：" private_host
            private_host=${private_host:-"192.168.1.59:10011"}
            read -p "仓库名称[myrepo]：" private_repo_name
            private_repo_name=${private_repo_name:-"myrepo"}

            sed -ri "s@http://192.168.1.59:10011@http://"${private_host}"@g" /opt/apache-maven/conf/settings.xml
            sed -ri "s@<id>probe</id>@<id>"${private_repo_name}"</id>@g" /opt/apache-maven/conf/settings.xml
        ;;
        *)
            printInfo "选择错误，默认使用阿里云仓库！"
            sed -ri "s@<activeProfile>dev</activeProfile>@<\!-- <activeProfile>dev</activeProfile> -->@g" /opt/apache-maven/conf/settings.xml
        ;;
    esac
    
    mvn -v
}

install && config